package com.example.map2thirdapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.btn)
    Button mBtn;


    //22.4389562533,111.9300842285
    double latitude = 22.4389562533;
    double longitude = 111.9300842285;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
//        startGaoDeMap();
        startBaiduMap();
    }


    //移动APP调起Android高德地图方式
    private void startGaoDeMap() {
        Intent intent = new Intent("android.intent.action.VIEW",
                android.net.Uri.parse("androidamap://navi?sourceApplication=amap&lat=" + latitude + "&lon=" + longitude + "&dev=1&style=0"));
        intent.setPackage("com.autonavi.minimap");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (isInstallByread("com.autonavi.minimap")) {
            startActivity(intent);
        } else {
            Toast.makeText(this, "没有安装高德地图客户端", Toast.LENGTH_SHORT).show();
        }
    }


    //移动APP调起Android百度地图方式
    private void startBaiduMap() {
        Intent intent = null;
        try {
            intent = Intent.getIntent("intent://map/direction?destination=latlng:" + latitude + "," + longitude + "|name:&origin=" + "我的位置" + "&mode=driving?ion=" + "我的位置" + "&referer=Autohome|GasStation#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isInstallByread("com.baidu.BaiduMap")) {
            startActivity(intent); // 启动调用
        } else {
            Toast.makeText(this, "没有安装百度地图客户端", Toast.LENGTH_SHORT).show();
        }
    }


    private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName)
                .exists();
    }


}